package com.fgxia.admin.controller.user;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.business.domain.bo.SysUserLoginCompanyBo;
import com.ruoyi.business.domain.bo.admin.AdminSysUserLoginCompanyBo;
import com.ruoyi.business.domain.vo.SysUserLoginCompanyVo;
import com.ruoyi.business.service.ISysUserLoginCompanyService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 公司信息
 *
 * @author 99
 * @date 2024-04-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/userLoginCompany")
public class AdminSysUserLoginCompanyController extends BaseController {

    private final ISysUserLoginCompanyService iSysUserLoginCompanyService;

    /**
     * 查询公司信息列表
     */
    @SaCheckPermission("business:userLoginCompany:list")
    @GetMapping("/list")
    public TableDataInfo<SysUserLoginCompanyVo> list(SysUserLoginCompanyBo bo, PageQuery pageQuery) {
        return iSysUserLoginCompanyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出公司信息列表
     */
    @SaCheckPermission("business:userLoginCompany:export")
    @Log(title = "公司信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysUserLoginCompanyBo bo, HttpServletResponse response) {
        List<SysUserLoginCompanyVo> list = iSysUserLoginCompanyService.queryList(bo);
        ExcelUtil.exportExcel(list, "公司信息", SysUserLoginCompanyVo.class, response);
    }

    /**
     * 获取公司信息详细信息
     *
     * @param driverId 主键
     */
    @SaCheckPermission("business:userLoginCompany:query")
    @GetMapping("/{driverId}")
    public R<SysUserLoginCompanyVo> getInfo(@NotNull(message = "主键不能为空")
                                            @PathVariable Long driverId) {


        return R.ok(iSysUserLoginCompanyService.queryById(driverId));
    }

    /**
     * 新增公司信息
     */
    @SaCheckPermission("business:userLoginCompany:add")
    @Log(title = "公司信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AdminSysUserLoginCompanyBo bo) {
        String checkRepeatInfo = iSysUserLoginCompanyService.checkRepeatInfo(bo);

        if (StringUtils.isNotBlank(checkRepeatInfo)) {
            return R.fail("新增公司信息'" + bo.getCompanyName() + "'失败，" + checkRepeatInfo);
        }

        return toAjax(iSysUserLoginCompanyService.insertByAdminBo(bo));
    }

    /**
     * 修改公司信息
     */
    @SaCheckPermission("business:userLoginCompany:edit")
    @Log(title = "公司信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AdminSysUserLoginCompanyBo bo) {
        String checkRepeatInfo = iSysUserLoginCompanyService.checkRepeatInfo(bo);

        if (StringUtils.isNotBlank(checkRepeatInfo)) {
            return R.fail("修改公司信息'" + bo.getCompanyName() + "'失败，" + checkRepeatInfo);
        }

        return toAjax(iSysUserLoginCompanyService.updateByAdminBo(bo));
    }

    /**
     * 删除公司信息
     *
     * @param driverIds 主键串
     */
    @SaCheckPermission("business:userLoginCompany:remove")
    @Log(title = "公司信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{driverIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] driverIds) {
        return toAjax(iSysUserLoginCompanyService.deleteWithValidByIds(Arrays.asList(driverIds), true));
    }
}
